library(tidyverse)
library(haven)
library(stargazer)
library(broom)
library(scales)
library(ggplot2)
library(kableExtra)



df <- read.csv("climate_lucid.csv")
######## By Respondent Party ID ------------------
#### Table 2: N per Condition #### 
df <- df[!is.na(df$pid3), ]
df <- df %>% mutate(repub  = ifelse(pid3=="Rep", 1, 0),
                    repub = factor(repub, levels=c("0", "1")),
                    repub = recode_factor(repub, '1' = "Republicans", '0' = "Non-Republicans"),
                    repub =  fct_relevel(factor(repub), "Republicans", "Non-Republicans"))

df %>% group_by(repub, local) %>%
  tally() %>%
  spread(repub ,n) %>% kbl(format="latex")%>% kable_styling(latex_option = "hold_position")

#### Figure 2 ####
rep <- df %>%  filter(repub=="Republicans")
norep <- df %>%  filter(repub=="Non-Republicans")

df.ttest <- data.frame(diff=c(t.test(Q8.2_1~local, rep)$estimate[2]-t.test(Q8.2_1~local, rep)$estimate[1]),
                       p.val = c(t.test(Q8.2_1~local, rep)$p.value))

p1 <- ggplot(rep, aes(local, Q8.2_1, fill= as.factor(local))) + 
  stat_summary(fun = mean, geom="bar", width=.75) + 
  stat_summary(fun.data = mean_cl_normal, geom= "errorbar", fun.args = list(conf.int = .90), width=0.1, alpha=1, size = 1) + 
  theme_bw() + scale_fill_manual(values = c("0" = "grey85", "1" = "grey55"),
                                 labels = c("National Media", "Local Media"),
                                 name = c("")) +
  theme(axis.text.x = element_blank(), legend.text = element_text(size = 18),
        axis.ticks.x = element_blank(), legend.position = "bottom",
        axis.text.y = element_text(size = 18),axis.title.y = element_text(size = 15)) + xlab("") + ylab("") +
  ggtitle("Accurate") + theme(text = element_text(size=12))+ coord_cartesian(ylim=c(0, 1)) + 
  geom_text(data=df.ttest, aes(x=0.5, y=0.85, label=paste("p-value:", round(p.val, 3))), 
            colour="black", inherit.aes=FALSE, parse=FALSE, size=5)+
  stat_summary(aes(hjust=-0.3, label=round(..y.., 2)), fun=mean, geom="text", size=5) 

p1

df.ttest <- data.frame(diff=c(t.test(Q8.4_1~local, rep)$estimate[2]-t.test(Q8.4_1~local, rep)$estimate[1]),
                       p.val = c(t.test(Q8.4_1~local, rep)$p.value))

p2 <- ggplot(rep, aes(local, Q8.4_1, fill= as.factor(local))) + 
  stat_summary(fun = mean, geom="bar", width=.75) + 
  stat_summary(fun.data = mean_cl_normal, geom= "errorbar", fun.args = list(conf.int = .90), width=0.1, alpha=1, size = 1) + 
  theme_bw() + scale_fill_manual(values = c("0" = "grey85", "1" = "grey55"),
                                 labels = c("National Media", "Local Media"),
                                 name = c("")) +
  theme(axis.text.x = element_blank(), legend.text = element_text(size = 18),
        axis.ticks.x = element_blank(), legend.position = "bottom",
        axis.text.y = element_text(size = 18),axis.title.y = element_text(size = 15)) + xlab("") + ylab("") +
  ggtitle("Relevant") + theme(text = element_text(size=12))+ coord_cartesian(ylim=c(0, 1)) + 
  geom_text(data=df.ttest, aes(x=0.5, y=0.85, label=paste("p-value:", round(p.val, 3))), 
            colour="black", inherit.aes=FALSE, parse=FALSE, size=5)+
  stat_summary(aes(hjust=-0.3, label=round(..y.., 2)), fun=mean, geom="text", size=5) 

p2

df.ttest <- data.frame(diff=c(t.test(serious ~local, rep)$estimate[2]-t.test(serious ~local, rep)$estimate[1]),
                       p.val = c(t.test(serious ~local, rep)$p.value))

p3 <- ggplot(rep, aes(local, serious, fill= as.factor(local))) + 
  stat_summary(fun = mean, geom="bar", width=.75) + 
  stat_summary(fun.data = mean_cl_normal, geom= "errorbar", fun.args = list(conf.int = .90), width=0.1, alpha=1, size = 1) + 
  theme_bw() + scale_fill_manual(values = c("0" = "grey85", "1" = "grey55"),
                                 labels = c("National Media", "Local Media"),
                                 name = c("")) +
  theme(axis.text.x = element_blank(), legend.text = element_text(size = 18),
        axis.ticks.x = element_blank(), legend.position = "bottom",
        axis.text.y = element_text(size = 18),axis.title.y = element_text(size = 15)) + xlab("") + ylab("") +
  ggtitle("Seriousness of Climate Change") + theme(text = element_text(size=12))+ coord_cartesian(ylim=c(0, 1)) + 
  geom_text(data=df.ttest, aes(x=0.5, y=0.85, label=paste("p-value:", round(p.val, 3))), 
            colour="black", inherit.aes=FALSE, parse=FALSE, size=5)+
  stat_summary(aes(hjust=-0.3, label=round(..y.., 2)), fun=mean, geom="text", size=5) 

p3

df.ttest <- data.frame(diff=c(t.test(action ~local, rep)$estimate[2]-t.test(action ~local, rep)$estimate[1]),
                       p.val = c(t.test(action ~local, rep)$p.value))

p4 <- ggplot(rep, aes(local, action, fill= as.factor(local))) + 
  stat_summary(fun = mean, geom="bar", width=.75) + 
  stat_summary(fun.data = mean_cl_normal, geom= "errorbar", fun.args = list(conf.int = .90), width=0.1, alpha=1, size = 1) + 
  theme_bw() + scale_fill_manual(values = c("0" = "grey85", "1" = "grey55"),
                                 labels = c("National Media", "Local Media"),
                                 name = c("")) +
  theme(axis.text.x = element_blank(), legend.text = element_text(size = 18),
        axis.ticks.x = element_blank(), legend.position = "bottom",
        axis.text.y = element_text(size = 18),axis.title.y = element_text(size = 15)) + xlab("") + ylab("") +
  ggtitle("Willingness to take action") + theme(text = element_text(size=12))+ coord_cartesian(ylim=c(0, 1)) + 
  geom_text(data=df.ttest, aes(x=0.5, y=0.85, label=paste("p-value:", round(p.val, 3))), 
            colour="black", inherit.aes=FALSE, parse=FALSE, size=5)+
  stat_summary(aes(hjust=-0.3, label=round(..y.., 2)), fun=mean, geom="text", size=5) 

p4

library(gridExtra)
library(ggpubr)
ggarrange(p1, p2, p3, p4, common.legend=TRUE, legend="bottom")


#### Non-Republican Analysis ####
df.ttest <- data.frame(diff=c(t.test(Q8.2_1~local, norep)$estimate[2]-t.test(Q8.2_1~local, norep)$estimate[1]),
                       p.val = c(t.test(Q8.2_1~local, norep)$p.value))

p1 <- ggplot(norep, aes(local, Q8.2_1, fill= as.factor(local))) + 
  stat_summary(fun = mean, geom="bar", width=.75) + 
  stat_summary(fun.data = mean_cl_normal, geom= "errorbar", fun.args = list(conf.int = .90), width=0.1, alpha=1, size = 1) + 
  theme_bw() + scale_fill_manual(values = c("0" = "grey85", "1" = "grey55"),
                                 labels = c("National Media", "Local Media"),
                                 name = c("")) +
  theme(axis.text.x = element_blank(), legend.text = element_text(size = 18),
        axis.ticks.x = element_blank(), legend.position = "bottom",
        axis.text.y = element_text(size = 18),axis.title.y = element_text(size = 15)) + xlab("") + ylab("") +
  ggtitle("Accurate") + theme(text = element_text(size=12))+ coord_cartesian(ylim=c(0, 1)) + 
  geom_text(data=df.ttest, aes(x=0.5, y=0.85, label=paste("p-value:", round(p.val, 3))), 
            colour="black", inherit.aes=FALSE, parse=FALSE, size=5)+
  stat_summary(aes(hjust=-0.3, label=round(..y.., 2)), fun=mean, geom="text", size=5) 

p1

df.ttest <- data.frame(diff=c(t.test(Q8.4_1~local, norep)$estimate[2]-t.test(Q8.4_1~local, norep)$estimate[1]),
                       p.val = c(t.test(Q8.4_1~local, norep)$p.value))

p2 <- ggplot(norep, aes(local, Q8.4_1, fill= as.factor(local))) + 
  stat_summary(fun = mean, geom="bar", width=.75) + 
  stat_summary(fun.data = mean_cl_normal, geom= "errorbar", fun.args = list(conf.int = .90), width=0.1, alpha=1, size = 1) + 
  theme_bw() + scale_fill_manual(values = c("0" = "grey85", "1" = "grey55"),
                                 labels = c("National Media", "Local Media"),
                                 name = c("")) +
  theme(axis.text.x = element_blank(), legend.text = element_text(size = 18),
        axis.ticks.x = element_blank(), legend.position = "bottom",
        axis.text.y = element_text(size = 18),axis.title.y = element_text(size = 15)) + xlab("") + ylab("") +
  ggtitle("Relevant") + theme(text = element_text(size=12))+ coord_cartesian(ylim=c(0, 1)) + 
  geom_text(data=df.ttest, aes(x=0.5, y=0.85, label=paste("p-value:", round(p.val, 3))), 
            colour="black", inherit.aes=FALSE, parse=FALSE, size=5)+
  stat_summary(aes(hjust=-0.3, label=round(..y.., 2)), fun=mean, geom="text", size=5) 

p2

df.ttest <- data.frame(diff=c(t.test(serious ~local, norep)$estimate[2]-t.test(serious ~local, norep)$estimate[1]),
                       p.val = c(t.test(serious ~local, norep)$p.value))

p3 <- ggplot(norep, aes(local, serious, fill= as.factor(local))) + 
  stat_summary(fun = mean, geom="bar", width=.75) + 
  stat_summary(fun.data = mean_cl_normal, geom= "errorbar", fun.args = list(conf.int = .90), width=0.1, alpha=1, size = 1) + 
  theme_bw() + scale_fill_manual(values = c("0" = "grey85", "1" = "grey55"),
                                 labels = c("National Media", "Local Media"),
                                 name = c("")) +
  theme(axis.text.x = element_blank(), legend.text = element_text(size = 18),
        axis.ticks.x = element_blank(), legend.position = "bottom",
        axis.text.y = element_text(size = 18),axis.title.y = element_text(size = 15)) + xlab("") + ylab("") +
  ggtitle("Seriousness of Climate Change") + theme(text = element_text(size=12))+ coord_cartesian(ylim=c(0, 1)) + 
  geom_text(data=df.ttest, aes(x=0.5, y=0.85, label=paste("p-value:", round(p.val, 3))), 
            colour="black", inherit.aes=FALSE, parse=FALSE, size=5)+
  stat_summary(aes(hjust=-0.3, label=round(..y.., 2)), fun=mean, geom="text", size=5) 

p3

df.ttest <- data.frame(diff=c(t.test(action ~local, norep)$estimate[2]-t.test(action ~local, norep)$estimate[1]),
                       p.val = c(t.test(action ~local, norep)$p.value))

p4 <- ggplot(norep, aes(local, action, fill= as.factor(local))) + 
  stat_summary(fun = mean, geom="bar", width=.75) + 
  stat_summary(fun.data = mean_cl_normal, geom= "errorbar", fun.args = list(conf.int = .90), width=0.1, alpha=1, size = 1) + 
  theme_bw() + scale_fill_manual(values = c("0" = "grey85", "1" = "grey55"),
                                 labels = c("National Media", "Local Media"),
                                 name = c("")) +
  theme(axis.text.x = element_blank(), legend.text = element_text(size = 18),
        axis.ticks.x = element_blank(), legend.position = "bottom",
        axis.text.y = element_text(size = 18),axis.title.y = element_text(size = 15)) + xlab("") + ylab("") +
  ggtitle("Willingness to take action") + theme(text = element_text(size=12))+ coord_cartesian(ylim=c(0, 1)) + 
  geom_text(data=df.ttest, aes(x=0.5, y=0.85, label=paste("p-value:", round(p.val, 3))), 
            colour="black", inherit.aes=FALSE, parse=FALSE, size=5)+
  stat_summary(aes(hjust=-0.3, label=round(..y.., 2)), fun=mean, geom="text", size=5) 

p4


library(gridExtra)
library(ggpubr)
ggarrange(p1, p2, p3, p4, common.legend=TRUE, legend="bottom")



